package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"
	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
	"net/url"
)

type BannerController struct{}

func (that *BannerController) Upload(ctx *fasthttp.RequestCtx) {

	filename, err := model.BannerUpload(ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, filename)
}

func (that *BannerController) Insert(ctx *fasthttp.RequestCtx) {

	data := model.TblBanner{}
	body := ctx.PostBody()

	err := json.Unmarshal(body, &data)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	admin := session.AdminGet(ctx)

	args := fasthttp.Args{}
	args.Parse(data.Images)

	data.Id = helper.GenId()
	data.State = "1"
	data.UpdatedUid = admin["id"]
	data.UpdatedName = admin["name"]
	data.UpdatedAt = uint32(ctx.Time().Unix())
	data.CreatedAt = data.UpdatedAt

	if data.Title != "" {
		decodedValue, err := url.QueryUnescape(data.Title)
		if err != nil {
			helper.RetData(ctx, false, "参数错误")
			return
		}
		data.Title = decodedValue
	}

	err = model.BannerInsert(data)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}

func (that *BannerController) List(ctx *fasthttp.RequestCtx) {

	page := ctx.QueryArgs().GetUintOrZero("page")
	pageSize := ctx.QueryArgs().GetUintOrZero("page_size")
	state := ctx.QueryArgs().GetUintOrZero("state")
	startTime := string(ctx.QueryArgs().Peek("start_time"))
	endTime := string(ctx.QueryArgs().Peek("end_time"))

	if page < 1 {
		page = 1
	}
	if pageSize < 10 {
		pageSize = 10
	}
	if pageSize > 200 {
		pageSize = 200
	}
	ex := g.Ex{}

	if state != 0 {
		ex["state"] = state
	}

	data, err := model.BannerList(uint(page), uint(pageSize), startTime, endTime, ex)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, data)
}

// 系统公告编辑
func (that *BannerController) Update(ctx *fasthttp.RequestCtx) {

	data := model.TblBanner{}
	body := ctx.PostBody()

	err := json.Unmarshal(body, &data)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	record := g.Record{}
	if data.Title != "" {
		decodedValue, err := url.QueryUnescape(data.Title)
		if err != nil {
			helper.RetData(ctx, false, "参数错误")
			return
		}
		record["title"] = decodedValue
	}

	if data.State != "" {
		record["state"] = data.State
	}
	if data.Seq != 0 {
		record["seq"] = data.Seq
	}

	if data.Images != "" {
		args := fasthttp.Args{}
		args.Parse(data.Images)

		//if !args.Has("h5") || !args.Has("web") {
		//	helper.RetData(ctx, false, "参数错误")
		//	return
		//}
		record["images"] = data.Images
	}

	if data.RedirectUrl != "" {
		decodedValue, err := url.QueryUnescape(data.RedirectUrl)
		if err != nil {
			helper.RetData(ctx, false, "参数错误")
			return
		}
		record["redirect_url"] = decodedValue
	}

	admin := session.AdminGet(ctx)

	record["url_type"] = data.UrlType
	record["updated_name"] = admin["name"]
	record["updated_uid"] = admin["id"]
	record["updated_at"] = uint32(ctx.Time().Unix())

	if len(record) == 0 {
		helper.RetData(ctx, false, helper.NoDataUpdate)
		return
	}

	err = model.BannerUpdate(data.Id, record)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}

// 系统停用 启用 系统审核
func (that *BannerController) UpdateState(ctx *fasthttp.RequestCtx) {

	id := string(ctx.QueryArgs().Peek("id"))
	if !helper.CtypeDigit(id) {
		helper.RetData(ctx, false, helper.IDErr)
		return
	}

	state := ctx.QueryArgs().GetUintOrZero("state")
	s := map[int]bool{
		1: true, //停用
		2: true, //启用
		3: true, //启用
	}
	if _, ok := s[state]; !ok {
		helper.RetData(ctx, false, "状态错误")
		return
	}

	err := model.BannerUpdateState(id, state)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}

func (that *BannerController) Delete(ctx *fasthttp.RequestCtx) {

	id := string(ctx.QueryArgs().Peek("id"))
	if !helper.CtypeDigit(id) {
		helper.RetData(ctx, false, helper.IDErr)
		return
	}

	err := model.BannerDelete(id)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}
